package com.dsy.agent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseAgent implements Agent {
    //日志
    private static final Logger logger = LoggerFactory.getLogger(BaseAgent.class);

    // 名称
    private String agentName;
    // 状态
    private Integer agentState=Agent.AGENT_STATE_FREE;
    private String systemPrompt;
    private String nextStepPrompt;
    private List<Message> messages=new ArrayList<>();

    @Override
    public String executeTask(String task) throws InterruptedException {
        if (task == null || "".equals(task)) {
            return "提示词不能为空！";
        }
        if (!agentState.equals(Agent.AGENT_STATE_FREE)) {
            return "当前状态为：" + agentState + "，请稍后再试！";
        }
        agentState = Agent.AGENT_STATE_BUSY;
        messages.add(new UserMessage(task));
        for (int i = 0; i < 20; i++) {
            String step = step();
            logger.info("第" + i + "步的结果是：" + step);
            if (agentState.equals(Agent.AGENT_STATE_FINISH)) {
                agentState = Agent.AGENT_STATE_FREE;
                break;
            }
        }
        return null;
    }

    public String getAgentName() {
        return agentName;
    }

    public void setAgentName(String agentName) {
        this.agentName = agentName;
    }

    public Integer getAgentState() {
        return agentState;
    }

    public void setAgentState(Integer agentState) {
        this.agentState = agentState;
    }

    public String getSystemPrompt() {
        return systemPrompt;
    }

    public void setSystemPrompt(String systemPrompt) {
        this.systemPrompt = systemPrompt;
    }

    public String getNextStepPrompt() {
        return nextStepPrompt;
    }

    public void setNextStepPrompt(String nextStepPrompt) {
        this.nextStepPrompt = nextStepPrompt;
    }

    public List<Message> getMessages() {
        return messages;
    }

    public void setMessages(List<Message> messages) {
        this.messages = messages;
    }

    public abstract String step() throws InterruptedException;
}
